﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.Framework.Net;
using ZyGames.Framework.RPC.Sockets;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Common.Serialization;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 上传头像
	/// </summary>
	public class Action10007 : BaseAction
	{
		private int _userId;
		private bool _result = false;
		private string _log = string.Empty;
		private string _headIcon;
		private GameUser _currentGameUser;

		public Action10007(HttpGet actionGetter)
			: base((short)ActionType.DZUploadHeadIcon, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("UserID", ref _userId) &&
				httpGet.GetString("HeadIcon", ref _headIcon))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			try
			{
				this._currentGameUser = GameTable.Current.GetUser(_userId);
				if (_currentGameUser == null)
				{
					Tool.WriteWarmingLine("10007当前玩家不存在");
					_log = "不存在该玩家";
					return true;
				}
				if (_headIcon.Length > 5000)
				{
					_log = "选择的头像太大,请重新选择上传";
					return true;
				}
				//玩家的头像另外存储
				//this._currentGameUser.ModifyLocked(() =>
				//{
				//	this._currentGameUser.HeadIconBytes = _headIcon;
				//});
				_result = GameTable.Current.AddUserHeadIcon(UserId, _headIcon);
				if (_result)
					_log = "修改成功";
				else
					_log = "修改失败";

				return true;
			}
			catch (Exception ex)
			{
				Tool.WriteErrorLine("10007消息异常:" + ex.ToString());
				return false;
			}
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(_result);
			this.PushIntoStack(_log);
		}
	}
}
